package com.my.core.util.geo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * geo工具类
 *
 * @author jeecg boot
 */
public class CoordinateConvertUtils {

    private static double PI = Math.PI;
    private static double AXIS = 6378245.0;
    private static double OFFSET = 0.006693421622965943;
    private static double X_PI;

    public CoordinateConvertUtils() {
    }

    public static void main(String[] args) {
        double[] g_b = gcj2BD09(35.772528, 115.025978);
        System.out.println(g_b[1] + "," + g_b[0]);

        String lngLat = "[[[106.39366,31.31224],[106.39225,31.31198]]]";
        String polygon = convertCoordinatesToPolygon(lngLat);
        System.out.println(polygon);
    }

    public static double[] gcj2BD09(double glat, double glon) {
        double[] latlon = new double[2];
        double z = Math.sqrt(glon * glon + glat * glat) + 2.0E-5 * Math.sin(glat * X_PI);
        double theta = Math.atan2(glat, glon) + 3.0E-6 * Math.cos(glon * X_PI);
        latlon[0] = z * Math.sin(theta) + 0.006;
        latlon[1] = z * Math.cos(theta) + 0.0065;
        return latlon;
    }

    public static double[] bd092GCJ(double glat, double glon) {
        double x = glon - 0.0065;
        double y = glat - 0.006;
        double[] latlon = new double[2];
        double z = Math.sqrt(x * x + y * y) - 2.0E-5 * Math.sin(y * X_PI);
        double theta = Math.atan2(y, x) - 3.0E-6 * Math.cos(x * X_PI);
        latlon[0] = z * Math.sin(theta);
        latlon[1] = z * Math.cos(theta);
        return latlon;
    }

    public static double[] bd092WGS(double glat, double glon) {
        double[] latlon = bd092GCJ(glat, glon);
        return gcj2WGS(latlon[0], latlon[1]);
    }

    public static double[] wgs2BD09(double wgLat, double wgLon) {
        double[] latlon = wgs2GCJ(wgLat, wgLon);
        return gcj2BD09(latlon[0], latlon[1]);
    }

    public static double[] wgs2GCJ(double wgLat, double wgLon) {
        double[] latlon = new double[2];
        if (outOfChina(wgLat, wgLon)) {
            latlon[0] = wgLat;
            latlon[1] = wgLon;
            return latlon;
        } else {
            double[] deltaD = delta(wgLat, wgLon);
            latlon[0] = wgLat + deltaD[0];
            latlon[1] = wgLon + deltaD[1];
            return latlon;
        }
    }

    public static double[] gcj2WGS(double glat, double glon) {
        double[] latlon = new double[2];
        if (outOfChina(glat, glon)) {
            latlon[0] = glat;
            latlon[1] = glon;
            return latlon;
        } else {
            double[] deltaD = delta(glat, glon);
            latlon[0] = glat - deltaD[0];
            latlon[1] = glon - deltaD[1];
            return latlon;
        }
    }

    public static double[] gcj2WGSExactly(double gcjLat, double gcjLon) {
        double initDelta = 0.01;
        double threshold = 1.0E-9;
        double mLat = gcjLat - initDelta;
        double mLon = gcjLon - initDelta;
        double pLat = gcjLat + initDelta;
        double pLon = gcjLon + initDelta;
        double i = 0.0;

        double wgsLat;
        double wgsLon;
        double[] latlon;
        do {
            wgsLat = (mLat + pLat) / 2.0;
            wgsLon = (mLon + pLon) / 2.0;
            latlon = wgs2GCJ(wgsLat, wgsLon);
            double dLat = latlon[0] - gcjLat;
            double dLon = latlon[1] - gcjLon;
            if (Math.abs(dLat) < threshold && Math.abs(dLon) < threshold) {
                break;
            }

            if (dLat > 0.0) {
                pLat = wgsLat;
            } else {
                mLat = wgsLat;
            }

            if (dLon > 0.0) {
                pLon = wgsLon;
            } else {
                mLon = wgsLon;
            }
        } while (!(++i > 10000.0));

        latlon = new double[]{wgsLat, wgsLon};
        return latlon;
    }

    public static double distance(double latA, double logA, double latB, double logB) {
        int earthR = 6371000;
        double x = Math.cos(latA * Math.PI / 180.0) * Math.cos(latB * Math.PI / 180.0) * Math.cos((logA - logB) * Math.PI / 180.0);
        double y = Math.sin(latA * Math.PI / 180.0) * Math.sin(latB * Math.PI / 180.0);
        double s = x + y;
        if (s > 1.0) {
            s = 1.0;
        }

        if (s < -1.0) {
            s = -1.0;
        }

        return Math.acos(s) * (double) earthR;
    }

    public static double[] delta(double wgLat, double wgLon) {
        double[] latlng = new double[2];
        double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
        double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
        double radLat = wgLat / 180.0 * PI;
        double magic = Math.sin(radLat);
        magic = 1.0 - OFFSET * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = dLat * 180.0 / (AXIS * (1.0 - OFFSET) / (magic * sqrtMagic) * PI);
        dLon = dLon * 180.0 / (AXIS / sqrtMagic * Math.cos(radLat) * PI);
        latlng[0] = dLat;
        latlng[1] = dLon;
        return latlng;
    }

    public static boolean outOfChina(double lat, double lon) {
        if (!(lon < 72.004) && !(lon > 137.8347)) {
            return lat < 0.8293 || lat > 55.8271;
        } else {
            return true;
        }
    }

    public static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * PI) + 320.0 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    public static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0;
        return ret;
    }

    static {
        X_PI = PI * 3000.0 / 180.0;
    }

    /**
     * 经纬度转多边形 [[[106.39366,31.31224],[106.39225,31.31198]]] ----> POLYGON ((106.39366 31.31224, 106.39225 31.31198))
     *
     * @param lngLat 经纬度数组
     */
    public static String convertCoordinatesToPolygon(String lngLat) {
        StringBuffer wkt = new StringBuffer();
        JSONArray jsonArray = JSONObject.parseArray(lngLat);
        wkt.append("POLYGON ").append(wktPolygon(jsonArray));
        return wkt.toString();
    }

    private static String wktPolygon(JSONArray jsonArray) {
        StringBuffer wkt = new StringBuffer();
        wkt.append("(");
        for (int i = 0; i < jsonArray.size(); i++) {
            //是一组组的LineString
            wkt.append("(");
            JSONArray lineString = jsonArray.getJSONArray(i);
            for (int j = 0; j < lineString.size(); j++) {
                JSONArray pointArray = lineString.getJSONArray(j);
                wkt.append(pointArray.getString(0)).append(" ").append(pointArray.getString(1)).append(",");
            }
            //删除最后一个逗号
            if (wkt.length() > 0) {
                wkt.deleteCharAt(wkt.length() - 1);
            }
            wkt.append("),");
        }
        if (wkt.length() > 0) {
            wkt.deleteCharAt(wkt.length() - 1);
        }
        wkt.append(")");
        return wkt.toString();
    }
}
